package com.yk103;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

import android.database.Cursor;
import android.provider.CallLog.Calls;
import android.text.format.DateFormat;

public class CallDurationComputer {
	public final static int DEF_DURATION_OFFSET = 2;
	private final static int BEGIN_DATE = 0;
	private final static int END_DATE = 1;
	private final static int YEAR_FILED = 0;
	private final static int MONTH_FILED = 1;
	private final static int DAY_FILED = 2;

	protected ArrayList<String> freeCallNumbers;
	private int[][] dates;
	private int durationOffset=DEF_DURATION_OFFSET;

	public CallDurationComputer() {
		freeCallNumbers = new ArrayList<String>();
		Calendar calendar = GregorianCalendar.getInstance();
		dates = new int[2][3];
		dates[BEGIN_DATE][YEAR_FILED] = calendar.get(Calendar.YEAR);
		dates[BEGIN_DATE][MONTH_FILED] = calendar.get(Calendar.MONTH);
		dates[BEGIN_DATE][DAY_FILED] = 1; // calendar.get(Calendar.DATE);

		dates[END_DATE][YEAR_FILED] = dates[BEGIN_DATE][YEAR_FILED]
				+ dates[BEGIN_DATE][MONTH_FILED] / Calendar.DECEMBER;
		dates[END_DATE][MONTH_FILED] = (dates[BEGIN_DATE][MONTH_FILED] + 1)
				% (Calendar.DECEMBER + 1);
		System.out.println(dates[BEGIN_DATE][MONTH_FILED]);
		System.out.println(dates[END_DATE][MONTH_FILED]);
		dates[END_DATE][DAY_FILED] = 1; // calendar.get(Calendar.)+dates[
		// BEGIN_DATE][DAY_FILED];
	}

	public int getYear(boolean beg) {
		if (beg) {
			return dates[BEGIN_DATE][YEAR_FILED];
		} else {
			return dates[END_DATE][YEAR_FILED];
		}
	}

	public int getDay(boolean beg) {
		if (beg) {
			return dates[BEGIN_DATE][DAY_FILED];
		} else {
			return dates[END_DATE][DAY_FILED];
		}
	}

	public int getMonth(boolean beg) {
		if (beg) {
			return dates[BEGIN_DATE][MONTH_FILED];
		} else {
			return dates[END_DATE][MONTH_FILED];
		}
	}

	public void setYear(boolean beg, int year) {
		if (beg) {
			dates[BEGIN_DATE][YEAR_FILED] = year;
		} else {
			dates[END_DATE][YEAR_FILED] = year;
		}
	}

	public void setDay(boolean beg, int day) {
		if (beg) {
			dates[BEGIN_DATE][DAY_FILED] = day;
		} else {
			dates[END_DATE][DAY_FILED] = day;
		}
	}

	public void setMonth(boolean beg, int month) {
		if (beg) {
			dates[BEGIN_DATE][MONTH_FILED] = month;
		} else {
			dates[END_DATE][MONTH_FILED] = month;
		}
	}

	public Date getDate(boolean beg) {
		Date date = null;
		if (beg) {
			date = new Date(dates[BEGIN_DATE][YEAR_FILED] - 1900,
					dates[BEGIN_DATE][MONTH_FILED],
					dates[BEGIN_DATE][DAY_FILED]);
		} else {
			date = new Date(dates[END_DATE][YEAR_FILED] - 1900,
					dates[END_DATE][MONTH_FILED], dates[END_DATE][DAY_FILED]);
		}
		return date;
	}

	public String getDateString(boolean beg) {
		Date date = getDate(beg);
		return (String) DateFormat.format("yyyy/MM/dd", date);
	}

	public void setDurationOffset(int offset) {
		durationOffset = offset;
	}

	public int compute(Cursor cursor) {
		int callTimeSum = 0;
		String number;
		long duration;
		long mSecond;
		Date date, begDate, endDate;
		int type;
		boolean freeCall;
		if (cursor == null || cursor.getCount() == 0) {
			return 0;
		}
		begDate = getDate(true);
		endDate = getDate(false);
		if (cursor.moveToFirst()) {
			while (true) {
				duration = cursor
						.getLong(cursor.getColumnIndex(Calls.DURATION));
				mSecond = cursor.getLong(cursor.getColumnIndex(Calls.DATE));
				type = cursor.getInt(cursor.getColumnIndex(Calls.TYPE));
				number = cursor.getString(cursor.getColumnIndex(Calls.NUMBER));
				if (type == Calls.OUTGOING_TYPE && duration != 0) {
					freeCall = false;
					for (int i = 0; i < freeCallNumbers.size(); i++) {
						if (freeCallNumbers.get(i).equals(number)) {
							freeCall = true;
							break;
						}
					}
					if (false == freeCall) {
						date = new Date(mSecond);
						if (date.compareTo(begDate) >= 0
								&& date.compareTo(endDate) < 0) {
							long minute, second;
							duration += durationOffset;
							minute = duration / 60;
							second = duration % 60;
							if (second != 0) {
								duration = (minute + 1) * 60;
							}
							callTimeSum += duration;
						}
					}
				}

				if (false == cursor.moveToNext()) {
					break;
				}
			}
			return callTimeSum / 60;
		}
		return 0;
	}
}
